<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepart(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <AddDept ref="addDep" :all-list="allList" :cur-node="curNode" :show-dialog="showDialog" @close-dialog="handleClose" @update-depart="hGetDepartments" />
    </div>
  </div>
</template>

<script>
// 引入接口
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept'
export default {
  components: {
    // 引入新增组件
    AddDept
  },
  data() {
    return {
      // 准备响应式数据
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false, // 控制新增组件的隐藏与显示
      curNode: '',
      allList: []
    }
  },
  // 调用method函数
  mounted() {
    this.hGetDepartments()
  },
  // 封装专门的method函数
  methods: {
    // 编辑用户信息
    editDepart(id) {
      // 调用子组件函数
      this.$refs.addDep.hGetDepartments(id)
      // 1.打开弹框
      this.showDialog = true
    },
    // 点击添加显示弹出层
    addPart(data) {
      // 展示弹框
      this.showDialog = true
      // 记录当前点击项的数据
      this.curNode = data
    },
    // 封装获取部门信息
    async hGetDepartments() {
      const data = await getDepartments()
      this.allList = data.depts
      this.list = tranListToTreeData(data.depts)
    },
    // 删除弹出层
    delPart(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delDepartments(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    // 关闭添加弹出层
    handleClose() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
